#!/usr/bin/env python
# coding: utf-8

# In[4]:


import pandas as pd
import os
import numpy as np
from sklearn.metrics import accuracy_score

print(os.getcwd())
print(os.listdir(os.getcwd()))
data = pd.read_csv('data.csv')
data.head()


# In[5]:


x = data.drop(['y'], axis=1)
x.head()


# In[6]:


y = data.loc[:, 'y']
y.head()


# In[7]:


# get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib import pyplot as plt

plt.figure(figsize=(10, 5))
passed = plt.scatter(x.loc[:, 'x1'][y == 1], x.loc[:, 'x2'][y == 1])

fialed = plt.scatter(x.loc[:, 'x1'][y == 0], x.loc[:, 'x2'][y == 0])

plt.xlabel('x1')
plt.ylabel('x2')
plt.title('raw data')
plt.legend((passed, fialed), ('passed', 'failed'))
plt.show()


# In[8]:


# 分离数据
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=10)
print(x_train.shape, x_test.shape)


# In[10]:


from keras.models import Sequential
from keras.layers import Dense, Activation

mlp = Sequential()

mlp.add(Dense(units=20, input_dim=2, activation='sigmoid'))
mlp.add(Dense(units=1, activation='sigmoid'))
mlp.summary()


# In[11]:


mlp.compile(optimizer='adam', loss='binary_crossentropy')


# In[ ]:


mlp.fit(x=x_train, y=y_train, epochs=8000)


# In[35]:


# 版本太高了，下面predict_classes这个用不了，已经被弃用了
# y_train_prediction = mlp.predict_classes(x_train)
y_train_prediction = (mlp.predict(x_train) > 0.5).astype(int)
# print(y_train_prediction)

# y_train_prediction_class = np.argmax(y_train_prediction, axis=1)


accuracy_rate = accuracy_score(y_train, y_train_prediction)
print(accuracy_rate)

print("================================================")

# 计算模型预测准确率
# y_train_predict = (mlp.predict(x_train) > 0.5).astype(int)
# y_train_predict = (MLP_model.predict(X_train) > 0.5).astype(int)
# accuracy_train = accuracy_score(y_train, y_train_predict)
# print("训练集准确率：", accuracy_train)

# y_test_predict = (mlp.predict(x_test) > 0.5).astype(int)
# y_test_predict = np.argmax(y_test_predict, axis=1)
# accuracy_test = accuracy_score(y_test, y_test_predict)
# print("测试集准确率：", accuracy_test)


# In[29]:


y_test_prindiction = mlp.predict(x_test)
y_test_prindiction_classes = np.argmax(y_test_prindiction, axis=1)
# print(y_test_prindiction_classes)
print(accuracy_score(y_test, y_test_prindiction_classes))

